<template >
    <rx-dialog @ok="okFun" @cancel="closeWindow" :okText="i18n('taskCheck.btnAssign','提交')" :btnAlign="'center'">
        <rx-fit>
            <template #middle>
              <div style="height:100%;width:100%;padding:5px;">
                <div>
                  <a-checkbox
                      v-model:checked="commuState.checkAll"
                      :indeterminate="commuState.indeterminate"
                      @change="onCheckAllChange"
                  >
                    {{i18n('taskExecutors.checkAll','全选')}}
                  </a-checkbox>
                </div>
                <a-divider />
                <a-checkbox-group :options="plainOptions" @change="selectChange"  v-model:value="commuState.checkedList"></a-checkbox-group>
              </div>
            </template>
        </rx-fit>
    </rx-dialog>
</template>

<script>
import {RxDialog, Util, RxFit} from '@lowcode/jpaas-base-lib'
import { mapState } from 'pinia';
import userUser from "@/stores/userUser";

export default {
    name: "withdraw-tips",
    computed: {
      ...mapState(userUser, ['user']),
    },
    components: {
        RxFit,
        RxDialog,
    },
    props: {
        commu: {type: Object, required: false},
        layerid: String,
        destroy: {
            type: Function
        }
    },
    data() {
        return {
            selectItem:{},
            plainOptions:[],
            commuState:{
                indeterminate: true,
                checkAll: false,
                checkedList: [],
            }
        }
    },
    created() {
        this.initCommuData();
    },
    methods: {
        onCheckAllChange(e){
            Object.assign(this.commuState, {
                checkedList: [],
                indeterminate: false,
            });
            if(this.commuState.checkAll){
                this.plainOptions.forEach(receiver=>{
                    this.commuState.checkedList.push(receiver.value);
                });
            }
        },
        initCommuData(){
            this.commu.receivers.forEach(receiver=>{
                if(1!=receiver.readed){
                    this.plainOptions.push({label:receiver.receiverName, value:receiver.id});
                }
            });
        },
        //多选变化
        selectChange(ary){
            if(ary.length==this.plainOptions.length){
                this.commuState.checkAll=true;
            }else {
                this.commuState.checkAll=false;
            }
        },
        okFun(btn){
            if(this.commuState.checkedList.length==0){
                btn.loading = false;
                this.$message.error(this.i18n("msa.selectUserTips","请勾选需要撤销的用户！"));
                return;
            }
            let all=false;
            if(this.commuState.checkAll && this.commu.receivers.length==this.commuState.checkedList.length){
                all=true;
            }
            let returnData={
                all:all,
                receiverIds:this.commuState.checkedList[0]
            }

            for(let i=1;i<this.commuState.checkedList.length;i++){
                returnData.receiverIds+=","+this.commuState.checkedList[i];
            }

            Util.closeWindow(this, "ok",returnData);
        },
        closeWindow(){
            Util.closeWindow(this, "cancel");
        },
        i18n(name,text,key){
            return this.$ti18(name,text,"bpmTask",key);
        },
    },
}
</script>

<style scoped lang="less">

</style>
